/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controle;

import DAO.NoticiaDAO;
import DAO.UsuarioDAO;
import Database.DBConnection;
import Model.Noticia;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Model.Usuario;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;

/**
 *
 * @author 0435104
 */
@WebServlet(name = "trataCadastroNoticia", urlPatterns = {"/trataCadastroNoticia"})
public class trataCadastroNoticia extends Comando {

    private Connection conexao;

    public void execute() throws ServletException, IOException, SQLException {
        getResponse().setContentType("text/html;charset=UTF-8");
        PrintWriter out = getResponse().getWriter();
        try {
             
            String titulo = getRequest().getParameter("titulo");
            String descricao = getRequest().getParameter("descricao");
            String link = getRequest().getParameter("link");
            Date dataCriacao = new Date(System.currentTimeMillis());
            String data = dataCriacao.toString();
 
            // Recupera o objeto de sessรฃo que identifica quem estรก publicando a noticia
 
            //Usuario value = (Usuario) session.getAttribute("usuario");
            HttpSession session = getRequest().getSession(false);
            Usuario value = (Usuario) session.getAttribute("usuario");
 
            Noticia noticia = new Noticia(titulo, descricao, link, data, value);
            new NoticiaDAO().inserir(noticia);
            
            out.println();
            out.println("<html> <head> <title> Sistema de Publicação de Noticias </title> </head>");
            out.println("<body>");
            out.println("<h1> Noticia cadastrada com sucesso ! </h1>");
            out.println("Em : " + data + "<p>");
            out.println("<P><a href='index.html'> Voltar  </a></P>");
            out.println("</body>");
            out.println("</html>");
            out.close();
             
        } catch (SQLException ex) {
            throw new ServletException(ex);
        } 
    }

}
